package com.soar.recruitment.module.user.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;

import com.soar.recruitment.R;
import com.soar.recruitment.common.dal.event.BaseEvent;
import com.soar.recruitment.common.ui.fragment.SOARBaseBackFragment;
import com.soar.recruitment.common.ui.view.MyEditView;
import com.soar.recruitment.common.ui.view.MyTextWatcher;
import com.soar.recruitment.common.ui.view.roundview.RoundTextView;
import com.soar.recruitment.common.util.Logs;
import com.soar.recruitment.common.util.SoarLoadDialog;
import com.soar.recruitment.module.user.bll.controller.ForgetContrats;
import com.soar.recruitment.module.user.bll.controller.ForgetPresenter;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;

/**
 * 忘记密码 - 手机号码验证 - 第一步
 * Created by chriswen on 2017/6/16.
 */
public class FotgetVertifyPhoneFragment extends SOARBaseBackFragment implements ForgetContrats.View {

    private ForgetContrats.Presenter mPresenter;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.phoneNumEditView)
    MyEditView phoneNumEditView;
    @BindView(R.id.codeNumEditView)
    MyEditView codeNumEditView;

    @BindView(R.id.rtv_next)
    RoundTextView rtv_next;

    public static FotgetVertifyPhoneFragment newInstance() {
        Bundle args = new Bundle();
        FotgetVertifyPhoneFragment fragment = new FotgetVertifyPhoneFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_forget_phone;
    }

    @Override
    public void initVariable() {
        mPresenter = new ForgetPresenter(this);
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    /**
     * start other BrotherFragment
     */
    @Subscribe
    public void onMainEvent(final BaseEvent event) {
        Logs.d("wplog", "onMainEvent ------ " + event);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                _mActivity.onBackPressed();
            }
        }, 100);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        initToolbarNavBlack(toolbar);
        toolbar.setTitle("忘记密码");

        codeNumEditView.setGetCodeType(false);
        setEditListener();

        rtv_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPresenter.resetPwdStep1();
            }
        });
    }

    private void setEditListener() {
        phoneNumEditView.addTextWatcher(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                codeNumEditView.setPhone(s.toString(), "1");
                codeNumEditView.setGetCodeStatus(!TextUtils.isEmpty(phoneNumEditView.getText()));
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public String getPhoneNum() {
        return phoneNumEditView.getText();
    }

    @Override
    public String getCodeNum() {
        return codeNumEditView.getText();
    }

    @Override
    public String getPwdNum() {
        return null;
    }

    @Override
    public void showLoading(String msg) {
        SoarLoadDialog.showLoadDialog(mActivity, msg);
    }

    @Override
    public void closeLoading() {
        SoarLoadDialog.closeLoadDialog();
    }

    @Override
    public void goNext() {
        start(FotgetResetPwdFragment.newInstance());
    }
}
